package com.knowledge.review.jucReview;

import lombok.Getter;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author XFW
 * @date 2024/6/21 14:41
 * @Description Lock 示例
 **/
public class LockExample {
    private final Lock lock = new ReentrantLock();
    @Getter
    private int counter = 0;

    public void increment() {
        lock.lock();
        try {
            counter++;
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        LockExample example = new LockExample();
        example.increment();
        // 输出：1
        System.out.println(example.getCounter());
    }
}
